package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entity.Contact;
import util.DBHelp;
import util.RowMapper;

public class ContactDao {

	
	private DBHelp<Contact> db=new DBHelp<Contact>();
	
	public void save(Contact c){
		String sql="INSERT INTO t_contact (id,c_name,company_name,mobile,tel,e_mail,address,website,weibo,content,createtime,project_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, c.getId(),c.getC_name(),c.getCompany_name(),c.getMobile(),c.getTel(),c.getE_mail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getCreatetime(),c.getProject_id());
	}
	
	public void update(Contact c){
		String sql="UPDATE t_contact SET c_name=?,company_name=?,mobile=?,tel=?,e_mail=?,address=?,website=?,weibo=?,content=? WHERE id=?";
		db.executeSQL(sql,c.getC_name(),c.getCompany_name(),c.getMobile(),c.getTel(),c.getE_mail(),c.getAddress(),c.getWebsite(),c.getWeibo(),c.getContent(),c.getId());
		
	}
	public void delete(String id){
		String sql="delete from t_contact where id=?";
		db.executeSQL(sql, id);
	}
	public List<Contact> findAll(String id){
		
		String sql="select id,c_name,company_name,mobile,tel,e_mail,address,website,weibo,content,createtime,project_id from t_contact where project_id=?";
		return db.executeQueryToList(sql,new ContactRowMapper(),id);
		
	}
	public class ContactRowMapper implements RowMapper<Contact>{

		public Contact mapRow(ResultSet rs) throws SQLException {
			Contact con=new Contact();
			con.setAddress(rs.getString("address"));
			con.setC_name(rs.getString("c_name"));
			con.setCompany_name(rs.getString("company_name"));
			con.setContent(rs.getString("content"));
			con.setCreatetime(rs.getString("createtime"));
			con.setE_mail(rs.getString("e_mail"));
			con.setId(rs.getString("id"));
			con.setMobile(rs.getString("mobile"));
			con.setProject_id(rs.getString("project_id"));
			con.setTel(rs.getString("tel"));
			con.setWebsite(rs.getString("website"));
			con.setWeibo(rs.getString("weibo"));
			
			return con;
		}
	}
	public Contact findById(String id) {
		String sql="select id,c_name,company_name,mobile,tel,e_mail,address,website,weibo,content,createtime,project_id from t_contact where id=?";
		return db.executeQueryToObject(sql, new ContactRowMapper(), id);
	}
}

